﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Tbl;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.DALFactory;
using System.Linq.Dynamic;
namespace HKEA.CMS.Core.DAL.Tbl
{
    public class tblAuditTrailDAL:ItblAuditTrail
    {
        HKEA_CMSEntities model;
        tblAuditTrail trail;

        public tblAuditTrailDAL()
        {
            model = DataAccess.GetModelInstance();
        }

        public static tblAuditTrailDAL GetInstance()
        {
            return new tblAuditTrailDAL();
        }

        public int InsertENT(tblAuditTrail ent)
        {
            ent.CreationDate = DateTime.Now;
            model.tblAuditTrails.AddObject(ent);
            return model.SaveChanges();
        }

        public List<tblAuditTrail> GetListByENT(tblAuditTrail ent, PageParam param, string strPropertyName,string strOrderBy)
        {
            var result = model.tblAuditTrails.Where(a => a.AuditTrailUID > 0);
            if (ent.AuditTrailUID > 0)
                result = result.Where(a => a.AuditTrailUID == ent.AuditTrailUID);
            param.Count = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip((param.CurrentPage - 1) * param.PageSize).Take(param.PageSize);
            return result.ToList();
        }
        public List<tblAuditTrail> GetListByENT(tblAuditTrail ent)
        {
            var result = model.tblAuditTrails.Where(a => a.AuditTrailUID > 0);
            if (ent.AuditTrailUID > 0)
                result = result.Where(a => a.AuditTrailUID == ent.AuditTrailUID);
            return result.ToList();
        }
        public int GetCountByENT(tblAuditTrail ent)
        {
            var result = model.tblAuditTrails.Where(a => a.AuditTrailUID > 0);
            if (ent.AuditTrailUID > 0)
                result = result.Where(a => a.AuditTrailUID == ent.AuditTrailUID);
            return result.Count();
        }

        public int UpdateENT(tblAuditTrail ent)
        {
            trail = model.tblAuditTrails.Where(a=>a.AuditTrailUID==ent.AuditTrailUID).FirstOrDefault();
            if (trail != null)
            {
                trail.AuditTrail_Detail = ent.AuditTrail_Detail;
                trail.AuditTrail_Field = ent.AuditTrail_Field;
                trail.AuditTrail_Tbl = ent.AuditTrail_Tbl;
                trail.AuditTrailCatUID = ent.AuditTrailCatUID;
                trail.ContactPersonUID = ent.ContactPersonUID;
                trail.CustomerUID = ent.CustomerUID;
                trail.LoginName = ent.LoginName;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(tblAuditTrail ent)
        {
            var temp = model.tblAuditTrails.Where(a => a.AuditTrailUID > 0);
            if (ent.AuditTrailUID > 0)
                temp = temp.Where(a=>a.AuditTrailUID==ent.AuditTrailUID);
            var list = temp.ToList();
            for (int i = 0; i < list.Count(); i++)
            {
                model.tblAuditTrails.DeleteObject(list[i]);
            }
            return model.SaveChanges();
        }
    }
}
